package com.ruoyi.chatroom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.chatroom.domain.ChatRecord;
import com.ruoyi.chatroom.domain.SendMessageEntity;
import com.ruoyi.chatroom.domain.bo.ChatRecordBo;
import com.ruoyi.chatroom.domain.vo.ChatRecordVo;
import com.ruoyi.chatroom.mapper.ChatRecordMapper;
import com.ruoyi.chatroom.service.IChatRecordService;
import com.ruoyi.chatroom.service.WebSocketService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 聊天记录Service业务层处理
 *
 * @author zhutianyu
 * @date 2021-09-03
 */
@Service
public class ChatRecordServiceImpl extends ServicePlusImpl<ChatRecordMapper, ChatRecord, ChatRecordVo> implements IChatRecordService {

    @Autowired
    WebSocketService webSocketService;

    @Override
    public ChatRecordVo queryById(String id) {
        return getVoById(id);
    }

    @Override
    public TableDataInfo<ChatRecordVo> queryPageList(ChatRecordBo bo) {
        PagePlus<ChatRecord, ChatRecordVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ChatRecordVo> queryList(ChatRecordBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<ChatRecord> buildQueryWrapper(ChatRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChatRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getSendUser()), ChatRecord::getSendUser, bo.getSendUser());
        lqw.eq(StrUtil.isNotBlank(bo.getReceiveUser()), ChatRecord::getReceiveUser, bo.getReceiveUser());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), ChatRecord::getContent, bo.getContent());
        List<Date> timeRange = bo.getSendTimeRange();
        if (timeRange != null) {
            lqw.between(CollectionUtil.isNotEmpty(timeRange), ChatRecord::getSendTime, timeRange.get(0), timeRange.get(1));
        }
        return lqw;
    }

    @Override
    public Boolean insertByBo(ChatRecordBo bo) {
        //消息存档
        ChatRecord add = BeanUtil.toBean(bo, ChatRecord.class);
        Date sendTime = new Date();
        add.setSendTime(sendTime);
        //发送消息
        SendMessageEntity entity = new SendMessageEntity();
        BeanUtils.copyProperties(add,entity);
        webSocketService.sendMessage(entity);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(ChatRecordBo bo) {
        ChatRecord update = BeanUtil.toBean(bo, ChatRecord.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ChatRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public List<ChatRecordVo> queryMessage(String sendUser, String receiveUser) {
        LambdaQueryWrapper<ChatRecord> lqw = Wrappers.lambdaQuery();
        List<String> users = Arrays.asList(sendUser, receiveUser);
        lqw.in(ChatRecord::getSendUser, users).in(ChatRecord::getReceiveUser, users);
        lqw.orderByAsc(ChatRecord::getSendTime);
        return listVo(lqw);
    }
}
